package br.com.transporte.controller;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import br.com.transporte.app.Principal;
import br.com.transporte.dao.VeiculoDAO;
import br.com.transporte.modelo.Veiculo;
import br.com.transporte.tabelas.TabelaVeiculo;
import javafx.beans.property.IntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class VeiculoController implements Initializable{
	@FXML
    private TableView<TabelaVeiculo> tabelaVeic;

    @FXML
    private TextField txCodigo;

    @FXML
    private Button btSalvar;

    @FXML
    private TableColumn clModelo;

    @FXML
    private TextField txBusca;

    @FXML
    private TextField txPlaca;

    @FXML
    private TextField txModelo;

    @FXML
    private Button btnEditar;

    @FXML
    private TableColumn clCombustivel;

    @FXML
    private Button btLimpar;

    @FXML
    private TableColumn clPlaca;

    @FXML
    private TableColumn clCodigo;

    @FXML
    private Button btnExcluir;

    @FXML
    private TextField txCombustivel;
    
    @FXML
    private Button btnMotorista;

    @FXML
    void eventSalvar(ActionEvent event) {
    	Veiculo v = new Veiculo();
    	v.setPlaca(txPlaca.getText());
    	v.setCombustivel(txCombustivel.getText());
    	v.setModelo(txModelo.getText());
    	VeiculoDAO dao = new VeiculoDAO();
    	
    	if(!txCodigo.getText().trim().equals("")){
    		v.setCodigo(Integer.parseInt(txCodigo.getText()));
    		dao.Editar(v);
    	}else{
    		dao.Inserir(v);    		
    	}
		carregarTabela();
    }

    @FXML
    void eventLimpar(ActionEvent event) {
    	txCodigo.setText("");
    	txPlaca.setText("");
    	txCombustivel.setText("");
    	txModelo.setText("");
    }

    @FXML
    void eventEditar(ActionEvent event) {
    	IntegerProperty cdVeiculo = tabelaVeic.getSelectionModel().getSelectedItem().codigoProperty();
    	
    	VeiculoDAO dao = new VeiculoDAO();
    	Veiculo vSalvo = dao.listarPorId(cdVeiculo.intValue());
    	txCodigo.setText(String.valueOf(vSalvo.getCodigo()));
    	txPlaca.setText(String.valueOf(vSalvo.getPlaca()));
    	txCombustivel.setText(vSalvo.getCombustivel());
    	txModelo.setText(vSalvo.getModelo());
    }

    @FXML
    void eventExcluir(ActionEvent event) {
    	int linhaSelecionada = tabelaVeic.getSelectionModel().getSelectedIndex();
    	IntegerProperty cdVeiculo = tabelaVeic.getSelectionModel().getSelectedItem().codigoProperty();
    	VeiculoDAO dao = new VeiculoDAO();
    	dao.Excluir(cdVeiculo.intValue());
    	tabelaVeic.getItems().remove(linhaSelecionada);
    }

    @FXML
    void eventPesquisar(ActionEvent event) {
    	VeiculoDAO dao = new VeiculoDAO();
		List<Veiculo> veiculos = dao.listarPlaca(txBusca.getText());
		ObservableList<TabelaVeiculo> dados = FXCollections.observableArrayList();
		
		for(Veiculo v: veiculos){
			dados.add(new TabelaVeiculo(v));
		}
		
		clCodigo.setCellValueFactory(new PropertyValueFactory("codigo"));
		clPlaca.setCellValueFactory(new PropertyValueFactory("placa"));
		clCombustivel.setCellValueFactory(new PropertyValueFactory("combustivel"));
		clModelo.setCellValueFactory(new PropertyValueFactory("modelo"));
		tabelaVeic.setItems(dados);
    }

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		carregarTabela();		
	}
	
	private void carregarTabela() {
		VeiculoDAO dao = new VeiculoDAO();
		List<Veiculo> veiculos = dao.listar();
		ObservableList<TabelaVeiculo> dados = FXCollections.observableArrayList();
		
		for(Veiculo v: veiculos){
			dados.add(new TabelaVeiculo(v));
		}
		
		clCodigo.setCellValueFactory(new PropertyValueFactory("codigo"));
		clPlaca.setCellValueFactory(new PropertyValueFactory("placa"));
		clCombustivel.setCellValueFactory(new PropertyValueFactory("combustivel"));
		clModelo.setCellValueFactory(new PropertyValueFactory("modelo"));
		tabelaVeic.setItems(dados);
	}
	
	@FXML
    void eventMotorista(ActionEvent event) {
		try{
			Parent root = FXMLLoader.load(getClass().getResource("/br/com/transporte/view/Motorista.fxml"));
			Principal.scene.setRoot(root);
		} catch(IOException ex){
			ex.printStackTrace();
		}
    }
}
